﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : ISalaryAllowance
    {
        public int SalaryAllowance_Using(int SalaryAllowanceID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowanceID", SalaryAllowanceID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet SalaryAllowance_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet SalaryAllowance_GetList(out int ItemCount, int currentPage, int pageSize)
        {


            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }

        public SalaryAllowanceInfo SalaryAllowance_GetInfo(int SalaryAllowanceID)
        {
            SalaryAllowanceInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_ID", SalaryAllowanceID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new SalaryAllowanceInfo();
                    retVal.ID = Convert.ToInt32(dr["SalaryAllowance_ID"]);
                    retVal.Name = dr["SalaryAllowance_Name"].ToString();
                    retVal.Type =Convert.ToInt32(dr["SalaryAllowance_Type"]);
                    retVal.Rate = dr["SalaryAllowance_Rate"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["SalaryAllowance_Rate"]);
                    retVal.Level = dr["SalaryAllowance_Level"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["SalaryAllowance_Level"]);

                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int SalaryAllowance_Insert(SalaryAllowanceInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Rate", eInfo.Rate);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Level", eInfo.Level);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool SalaryAllowance_Update(SalaryAllowanceInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Type", eInfo.Type);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Rate", eInfo.Rate);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Level", eInfo.Level);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool SalaryAllowance_Delete(int SalaryAllowanceID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_ID", SalaryAllowanceID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool SalaryAllowance_Exist(int _ID, string _Name, int _Type)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_SalaryAllowance_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_ID", _ID);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Name", _Name);
            dbCmd.Parameters.AddWithValue("@SalaryAllowance_Type", _Type);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
    }
}
